var api = require("../../api.js"), app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {

        // 表单
        car_list: [{
                'til': '车架号',
                'placeholder': '未填写',
                'value': '',
                'name': 'frame_num'
            },
            {
                'til': '发动机编号',
                'placeholder': '未填写',
                'value': '',
                'name': 'engine_num'
            },
            {
                'til': '车主姓名',
                'placeholder': '未填写',
                'value': '',
                'name': 'owner'
            },
            {
                'til': '车牌号',
                'placeholder': '未填写',
                'value': '',
                'name': 'plate_num'
            },
            {
                'til': '购车价格',
                'placeholder': '未填写',
                'value': '100000',
                'name': 'price'
            },
            {
                'til': '购车日期',
                'placeholder': '未填写',
                'value': '2018-08-08',
                'name': 'buy_time'
            },
            {
                'til': '总里程',
                'placeholder': '未填写',
                'value': '小于5000km',
                'name': 'total_mileage'
            },
        ]

    },

    // to 选择车系
    toseries:function(){
        wx.navigateTo({
            url: '/pages/choose_series/choose_series',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },

    // 获取 单个 input
    input_change: function(t) {
        console.log(t);
        console.log("=======================");

        var that = this;
        var e = t.target.dataset.index;
        var o = this.data.car_list;
        o[e].value = t.detail.value;
        console.log(o[e].value)
        console.log(t.detail.value)
        that.setData({
            car_list: o
        })
        console.log(o)
    },
    // form submit
    bind_submit: function(t) {
        let that = this;
        let series_id = that.data.seriesr_id;
        let model_id = that.data.model_id;
        
        let value = t.detail.value;
        let buy_time = value.buy_time;
        let frame_num = value.frame_num;
        let engine_num = value.engine_num;
        let owner = value.owner;
        let plate_num = value.plate_num;
        let price = value.price;
        let total_mileage = value.total_mileage;

        app.request({
          url: api.car.submit,
            method: "POST",
            data: {
                series_id: series_id,
                model_id: model_id,
                frame_num: frame_num,
                engine_num: engine_num,
                owner: owner,
                plate_num: plate_num,
                price: price,
                buy_time: buy_time,
                total_mileage: total_mileage
            },
            success: function(res) {
                console.log(res)
                if (res.code == 1) {
                    wx.showModal({
                        title: '信息错误',
                        content: '请填写完整登记表信息',
                    })
                } else {
                    // 提交成功后跳转到 列表页面
                    wx.redirectTo({
                        url: '/pages/car_list/car_list',
                    })
                }
            },
            fail: function(res) {},
            complete: function(res) {},
        })




    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // 拿到上一页传过来的 id
        let that = this;
        app.request({
          url: api.car.series,
            data: {},
            method: 'GET',
            success: function (res) {
                console.log(res[0])
                let car_lsit = res[0];
                that.setData({
                    _list: res[0]
                })
               
                that.setData({
                  series_list: car_lsit
                })
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    

    //choose series_car
    series_car: function(e) {
        let that = this;
        let car_list = that.data._list;
        let seriesr_id = car_list[e.detail.value].id;
        console.log(seriesr_id)
        console.log("=================================================")
        let keyword = car_list[e.detail.value].series_name;
        console.log(keyword)
        that.setData({
            series_inndex: e.detail.value,
            seriesr_id: seriesr_id,
            keyword: keyword
        })

        // Top choose series_car
        app.request({
            url: api.car.model,
            data: {
                keyword: keyword
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res[0])

                // model_list
                let md_list = [];
                let model_list = res[0];
                for (var j = 0; j < model_list.length; j++) {
                    md_list.push(model_list[j].model_name)
                    console.log(model_list[j].model_name)
                }
                console.log(md_list)
                that.setData({
                    model_list: res[0],
                    md_list: md_list
                })
            },
        })




    },
    // choose model
    model_list: function(e) {
        let that = this;
        console.log(e)
        let model_index = e.detail.value
        let series_id = that.data.seriesr_id;
        let model_id = that.data.model_list[model_index].id;
        console.log(series_id)
        console.log(model_id)
        console.log('model_id+++++++++++++++++++++++++++')
        console.log("=+++++++++++++++++++++")
        that.data.md_list
        console.log(that.data.md_list)
        that.setData({
            model_index: model_index,
            model_id: model_id
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})